package monkey;

import food.Food;
import idea.Idea;

import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

import view.Environnement;
import assets.Vision;

/**
 * 
 * @author Steel Magnolias
 * Nathalie GLAD
 * Ailin MORENO
 * 
 * Type de singe
 *
 */
public class Lambda extends Monkey{
	
	boolean cheat = false;
	int count;

	public Lambda(Vision champVision, int hungry, Point start, Environnement e) {
		super(champVision, hungry, start, e);
		BufferedImage img;
		try {
			img = ImageIO.read(this.getClass().getResource("lambda.png"));
		} catch (IOException ex) {
			ex.printStackTrace();
			img = null;
		}
        this.representation = img;
        this.currentState = Monkey.DOMINE;
	}

	@Override
	public void cheatingOrEating(int fi) {
		Graphics g = this.representation.getGraphics();
		Graphics2D g2d = (Graphics2D)g;
		if (cheat && e.cheat){
			count++;
			hungry --;
			Idea i = new Idea("ideeTromperie");
			g.drawImage(i.getRepresentation(), 40, 0, e);	
			if (count%3==0){
				g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
				g2d.fillRect(40, 0, representation.getWidth()-40, representation.getHeight());
				count=0;
			}
		}
		else{
			count = 0;
			g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
			g2d.fillRect(40, 0, representation.getWidth()-40, representation.getHeight());
			moveTo(e.allFoodList.get(fi).getPosition());
			if (position.equals(e.allFoodList.get(fi).getPosition())){
				eat(fi);
			}
		}
		g.dispose();
	}

	@Override
	public int lookForFood() {
		Monkey dominant = e.monkeysList.get(0);
		for (int i = 0; i < e.allFoodList.size(); i++) {
			Food vf = e.allFoodList.get(i);
			if (getChampVision().getWay() > 0) {
				if (vf.getPosition().x > getChampVision().getStartPoint()
						.x
						&& vf.getPosition().x < getChampVision()
								.getEndPointA().x
						&& vf.getPosition().y < getChampVision()
								.getEndPointA().y
						&& vf.getPosition().y > getChampVision()
								.getEndPointB().y) {
					if (dominant.getPosition().x > getChampVision().getStartPoint().x
							&& dominant.getPosition().x < (getChampVision().getEndPointA().x + VISION_DOMINANT)
							&& dominant.getPosition().y < getChampVision().getEndPointA().y
							&& dominant.getPosition().y > getChampVision().getEndPointB().y
							&& vf.getFoodState() == Food.HIDDEN_FOOD){
						if (e.cheat)
							cheat = true;
						else
							return -1;
					}
					else 
						cheat = false;
					return i;
					
				}
			} else {
				if (vf.getPosition().x < getChampVision().getStartPoint()
						.x
						&& vf.getPosition().x > getChampVision()
								.getEndPointA().x
						&& vf.getPosition().y < getChampVision()
								.getEndPointA().y
						&& vf.getPosition().y > getChampVision()
								.getEndPointB().y) {
					if (dominant.getPosition().x < getChampVision().getStartPoint().x
							&& dominant.getPosition().x > (getChampVision().getEndPointA().x - VISION_DOMINANT)
							&& dominant.getPosition().y < getChampVision().getEndPointA().y
							&& dominant.getPosition().y > getChampVision().getEndPointB().y
							&& vf.getFoodState() == Food.HIDDEN_FOOD){
						if (e.cheat)
							cheat = true;
						else
							return -1;
					}
						
					else 
						cheat = false;
					return i;
				}
			}
		}
		return -1;
	}
	
}
